package com.lyj.io.module.generator.controller;

import com.lyj.io.annotation.SysLogAnnotation;
import com.lyj.io.common.utils.R;
import com.lyj.io.module.generator.dto.CodeDto;
import com.lyj.io.module.generator.service.SysGeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 代码生成器api
 * @author:liyajie
 * @createTime:2021/12/28 0:52
 * @version:1.0
 */
@Api(value = "GeneratorController", tags = "代码生成器api")
@RestController
@RequestMapping("/api/v1/code")
public class GeneratorController {

    @Autowired
    SysGeneratorService sysGeneratorService;

    /**
     * 生成代码
     * @author: liyajie
     * @date: 2021/12/30 16:51
     * @param ids
     * @param response
     * @return com.lyj.io.common.utils.R
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @ApiOperation(value = "生成代码", notes = "生成代码")
    @GetMapping("/generatorCode")
    public void generatorCode(String ids, HttpServletResponse response){
        try{
            byte[] data = sysGeneratorService.generatorCode(ids.split(","));
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"generatorCode.zip\"");
            response.addHeader("Content-Length", "" + data.length);
            response.setContentType("application/octet-stream; charset=UTF-8");
            IOUtils.write(data, response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
